package com.atguigu.gmall.common.utils;

import com.atguigu.gmall.common.constant.RedisConst;
import org.springframework.util.StringUtils;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

/**
 * @ClassName RequestUtils
 * @Description TODO
 * @Author king
 * @Date 2022/11/17 18:34
 * @Version 1.0
 */
public class RequestUtils {
    public static HttpServletRequest getRequest(){
        //获取springMVC利用监听器给当前线程放好的请求对象
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        //拿到原生请求
        HttpServletRequest request = attributes.getRequest();

        return request;
    }

    public static Long uid(){
        HttpServletRequest request = getRequest();
        String uid = request.getHeader(RedisConst.UID_HEADER);
        if (!StringUtils.isEmpty(uid)){
            long l = Long.parseLong(uid);
            return l;
        }
        return null;
    }

    public static String tempid(){
        HttpServletRequest request = getRequest();
        String tempid = request.getHeader(RedisConst.U_TEMP_ID_HEADER);
        return tempid;
    }
}
